home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / blkade11.zip / DEMO.C < prev    next >
C/C++ Source or Header  |  1991-09-11  |  6KB  |  170 lines

  1.  
  2. /* -------------------------------------------------------------- */
  3. /*            Blockade(tm) demonstration program.                 */
  4. /*                                                                */
  5. /*  Demonstrates the use of the blockade self checking routines.  */
  6. /*                                                                */
  7. /*   1.03 - automatically clear patch area if /O specified.       */
  8. /*   1.02 - added more comments and cleaned up a little.          */
  9. /*   1.01 - added patch overide /O function to accomodate MSC     */
  10. /*          which places a value in the exe header chksum.        */
  11. /*                                                                */
  12. /* -------------------------------------------------------------- */
  13.  
  14. #include "allstds.h"             /* Standard stuff  */
  15. #ifdef __TURBOC__                /* defined in BC++ */
  16.  #pragma hdrstop
  17. #endif
  18.  
  19. #include "blkproto.h"            /* function prototypes for the library */
  20. #include "signatur.h"            /* holds the signature string          */
  21.  
  22. /* ------------------------------------------- */
  23.  
  24. int main( int argc,char * *argv );
  25. void do_args( int, char **);
  26. int patch_checksum( char *, unsigned );
  27.  
  28. /* ------------------------------------------- */
  29.  
  30. int fix_mode = 0;              /* 1 = patch to fix... */
  31. int corrupt_mode = 0;          /* 1 = Corrupt to force error */
  32. unsigned fix_value;            /* value to use to patch      */
  33.  
  34. #ifdef __TURBOC__
  35. int patch_overide = 0;         /* There shouldn't be any value  */
  36. #else
  37. int patch_overide = 1;         /* But MSC uses the checksum area ?! */
  38. #endif
  39.  
  40. /* ------------------------------------------- */
  41.  
  42. main(int argc,char **argv)
  43. {
  44.  int stat;
  45.  char *sp;
  46.  
  47.  printf("\n");
  48.  printf("BLOCKADE (tm) demo program (%s). Ver. 1.03 \n", argv[0]);
  49.  printf("   Copyright (C) 1991 Indusoft Corp. \n\n");
  50.  printf(" Switches:\n");
  51.  printf("    /C  - corrupt myself so self check fails.\n");
  52.  printf("    /F  - fix myself so self check passes.\n\n");
  53.  
  54.  do_args( argc, argv);
  55.  
  56.  if (fix_mode || corrupt_mode)
  57.         { stat = patch_checksum( argv[0], fix_value);  }
  58.  
  59.  printf("Checking myself for changes..\n");
  60.  
  61.  stat = blockade( argv[0], 780);       /* the heart of the demo.. */
  62.  
  63.  printf ("Results of file self check = %d\n", stat);
  64.  
  65.  sp = get_blk_err_msg( stat);
  66.  printf("Status message from file self check was \"%s\". \n", sp);
  67.  if (stat == 7)
  68.     printf("Run \"Brand DEMO\" to initialize this file and retry..\n");
  69.  return(0);
  70.  }
  71.  
  72. /*-------------------------------------------------------------------*/
  73. /*  Gathers up the command line switches.                            */
  74. /*                                                                   */
  75. /*-------------------------------------------------------------------*/
  76.  
  77. void do_args( int argc, char **argv)
  78.  {
  79.    char buf[4], *sp;
  80.    int j;
  81.  
  82.   for (j = 1; j < argc; j++)
  83.     {
  84.       sp = buf;
  85.       strncpy( buf, argv[j], sizeof(buf)-1);
  86.       strupr( buf );
  87.       if (*sp =='-') *sp = '\\';       /* accommodate those unix guys..*/
  88.       if (*sp =='/') sp++;             /* look to next char...         */
  89.       else           continue;         /* nothing I know how to handle */
  90.  
  91.       if (*sp == 'F')
  92.            {  fix_mode = 1;     fix_value = 0;  }
  93.       if (*sp == 'C')
  94.            {  corrupt_mode = 1; fix_value = 1;  }
  95.       if (*sp == 'O')
  96.            {  patch_overide = 1;
  97.               fix_mode = 1;     fix_value = 0;  }
  98.    }
  99.  }
  100.  
  101. /* -------------------------------------------------------------- */
  102.  
  103. #pragma pack(1)                /* MSC only, ignored by TC/BC */
  104.  
  105. typedef struct exeheadr {       /* Structure of .EXE file header */
  106.        unsigned char sig[2];
  107.        unsigned mod512;
  108.        unsigned num_pages;
  109.        unsigned reloc_cnt;
  110.        unsigned hdr_size;
  111.        unsigned min_para;
  112.        unsigned max_para;
  113.        unsigned stack_seg;
  114.        unsigned start_sp;
  115.        unsigned checksum;        /* here's where we patch... */
  116.        unsigned entry_ip;
  117.        unsigned code_seg;
  118.        unsigned relo_offset;
  119.        unsigned short    ovl_num;
  120.        unsigned short    relo_stuff[80];
  121.        } EXEHDR;
  122.  
  123.  EXEHDR   exehdr;
  124.  
  125. /* --------------------------------------------------------------- */
  126.  
  127. int patch_checksum( char *infile, unsigned value)
  128. {
  129.  size_t stat, check, ok_to_patch = 1;
  130.  FILE *fd;
  131.  
  132.  if (fix_mode)      printf("Zeroing DEMO file patch area...\n");
  133.  if (corrupt_mode)  printf("Corrupting DEMO file...\n");
  134.  
  135.  if ((fd = fopen(infile,"r+b")) == NULL)
  136.   {
  137.      fprintf(stderr,"Unable to open %s .\n",infile);
  138.      return(2);
  139.   }
  140.  
  141.   stat = fread( (void *)&exehdr, 1, sizeof(exehdr), fd);
  142.   if (stat != sizeof(exehdr))
  143.       {  printf ("Error reading exe header of demo file\n");     }
  144.  
  145.   if ((exehdr.sig[0] != 'M') || (exehdr.sig[1] != 'Z'))
  146.       {
  147.          printf ("Error, not a valid EXE file signature.\n");
  148.       }
  149.  
  150.   check = exehdr.checksum;
  151.   if (check != 0 && check != 1)
  152.     {
  153.       printf("Warning unexpected value (%u) found in Demo patch area.\n",check);
  154.       if (!patch_overide) ok_to_patch = 0;
  155.     }
  156.  
  157.   if (ok_to_patch)
  158.            exehdr.checksum = value;
  159.  
  160.   rewind(fd);
  161.   stat = fwrite( (void *)&exehdr, 1, sizeof(exehdr), fd);
  162.   if (stat != sizeof(exehdr))
  163.       {  printf ("Error writing to demo file. \n");     }
  164.  
  165.   fclose(fd);
  166.   return(0);
  167. }
  168.  
  169. /* ------------------------------------------------------ */
  170.